package com.taotao.springboot2.warmflow;

import com.taotao.springboot2.common.RespData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dromara.warm.flow.core.FlowFactory;
import org.dromara.warm.flow.core.dto.FlowParams;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.enums.SkipType;
import org.dromara.warm.flow.core.keygen.SnowFlakeId14;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.core.service.InsService;
import org.dromara.warm.flow.core.service.TaskService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;


@Api(tags = "工作流测试")
@RestController
@RequestMapping("/warmflow")
public class WarmflowController {

    @Resource
    private DefService defService;

    @Resource
    private InsService insService;

    @Resource
    private TaskService taskService;


    public String getFlowCode() {
        return "serial55";
    }

    public FlowParams getUser() {
        return FlowParams.build().flowCode(getFlowCode())
                .handler("1")
                .skipType(SkipType.PASS.getKey())
                .permissionFlag(Arrays.asList("role:1", "role:2"));
    }


    @ApiOperation(value = "启动流程", notes = "")
    @GetMapping("/startFlow")
    public RespData<String> startFlow() {

        SnowFlakeId14 sf = new SnowFlakeId14(123243453L);
        Instance instance = insService.start(sf.nextId() + "", getUser());
        System.out.println("已开启的流程实例id：" + instance.getId());
        taskService.list(FlowFactory.newTask().setInstanceId(instance.getId()))
                .forEach(task -> System.out.println("流转后任务id实例：" + task.getId()));
        return RespData.successResp(null);
    }

}
